#include "sk_reg_info_edit.h"
#include "ui_sk_reg_info_edit.h"

#include "pub_var.h"
#include <QSettings>
#include <QMessageBox>

Sk_reg_info_edit::Sk_reg_info_edit(QDialog *parent) :
    QDialog(parent),
    ui(new Ui::Sk_reg_info_edit)
{
    ui->setupUi(this);

    qDebug()<<"Sk_reg_info_edit initial\n";

    //恢复当前窗口位置
    QSettings setIni("clin.ini", QSettings::IniFormat);
    QRect rct = setIni.value("windowState/reg_info_edit").toRect();
    this->setGeometry(rct);

    //数据库连接
    qro = new QSqlQuery(pub::db_o);
    qrot = new QSqlQuery(pub::db_o);

    qDebug()<<"Sk_reg_info_edit initial finished.\n";
}

Sk_reg_info_edit::~Sk_reg_info_edit()
{
    delete ui;
    qDebug()<<"Sk_reg_info_edit will destroy\n";
}

void Sk_reg_info_edit::on_pb_exit_clicked()
{
    close();

    qDebug()<<"Sk_reg_info_edit closed.\n";
}

void Sk_reg_info_edit::closeEvent(QCloseEvent *event)
{
    Q_UNUSED(event);
    QSettings setIni("clin.ini", QSettings::IniFormat);
    setIni.setValue("windowState/reg_info_edit", QVariant(this->geometry()));
}

void Sk_reg_info_edit::on_pb_search_clicked()
{

    qDebug()<<"on_pb_search_clicked\n";

    QString sq;
    if(ui->rb_iden->isChecked())
    {
        //按身份证检索
        sq = " select * from fin_opr_register "
             " where idenno = '%1'";
        sq = sq.arg(ui->le_search->text().trimmed());
    }
    else
    {
        //按病志号检索
        sq = " select * from fin_opr_register "
             " where card_no = '%1'";
        sq = sq.arg( pub::add0( ui->le_search->text().trimmed(), 10));
    }

    qDebug()<<"search sql is : "<<sq<<"\n";

    qro->exec(sq);

    if(!qro->next())
    {
        QMessageBox::information(this, "注意", "指定的身份证或病志号无效！");
        qDebug()<<"on_pb_search_clicked finished with no result\n";
        return;
    }

    ui->le_name->setText(qro->value("name").toString());
    ui->le_iden->setText(qro->value("idenno").toString());
    ui->le_card_no->setText(qro->value("card_no").toString());
    ui->de_birth->setDate(qro->value("birthday").toDate());
    QString gend = qro->value("sex_code").toString();
    if(gend == "F")
    {
        ui->rb_gend_f->setChecked(true);
    }
    else
    {
        ui->rb_gend_m->setChecked(true);
    }

    qDebug()<<"on_pb_search_clicked finish\n";
}

void Sk_reg_info_edit::on_pb_save_clicked()
{

    qDebug()<<"on_pb_save_clicked\n";

    if(!input_valid())
    {
        return;
    }

    QString sq;
    sq = " update fin_opr_register "
         " set name = '%1', "
         " card_no = '%2', "
         " idenno = '%3', "
         " birthday= to_date('%4','yyyy-mm-dd'), "
         " sex_code = '%5'";

    QString sex_code;

    sq = sq.arg(ui->le_name->text().trimmed())
            .arg(pub::add0( ui->le_card_no->text().trimmed(), 10))
            .arg(ui->le_iden->text().trimmed())
            .arg(ui->de_birth->date().toString("yyyy-MM-dd"))
            .arg(ui->rb_gend_f->isChecked() ? "F" : "M");

    if(ui->rb_iden->isChecked())
    {
        //按身份证
        sq += " where idenno = '" + ui->le_search->text().trimmed() + "'";
    }
    else
    {
        //按病志号
        sq += " where card_no = '"
                + pub::add0( ui->le_card_no->text().trimmed(), 10) + "'";
    }

    qDebug()<<"modify sql is : "<<sq<<"\n";

    if(qro->exec(sq))
    {
        QMessageBox::information(this, "注意", "信息修改成功！");
        input_clear();
    }
    else
    {
        QMessageBox::information(this, "注意", "信息修改 sql 执行失败！");
    }


}


bool Sk_reg_info_edit::input_valid()
{
    if(ui->le_iden->text().trimmed().isEmpty())
    {
        QMessageBox::information(this, "注意", "身份证号不可以空缺！");
        return false;
    }

    if(ui->le_card_no->text().trimmed().isEmpty())
    {
        QMessageBox::information(this, "注意", "病志号不可以空缺！");
        return false;
    }

    if(ui->le_name->text().trimmed().isEmpty())
    {
        QMessageBox::information(this, "注意", "姓名不可以空缺！");
        return false;
    }

    if(ui->de_birth->date() > QDate::currentDate())
    {
        QMessageBox::information(this, "注意", "出生日期无效！");
        return false;
    }

    return true;
}

void Sk_reg_info_edit::input_clear()
{
    ui->le_iden->clear();
    ui->le_card_no->clear();
    ui->le_name->clear();
    ui->le_search->clear();
}
